/*
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __ELECTR_BOARD_ABILITY_SLICE_H__
#define __ELECTR_BOARD_ABILITY_SLICE_H__

#include "common.h"
#include "event_listener.h"
#include "native_base.h"
#include "schedule_info.h"
#include "schedule_list_view.h"
#include "schedule_server.h"
#include "sstream"
#include "stdio.h"
#include "string.h"
#include "ui_config.h"
#include "weather_time_view.h"
#include <ability_info.h>
#include <ability_loader.h>
#include <bundle_info.h>
#include <common/task.h>
#include <components/root_view.h>
#include <components/ui_image_view.h>
#include <components/ui_label.h>
#include <components/ui_label_button.h>
#include <components/ui_scroll_view.h>
#include <components/ui_swipe_view.h>
#include <components/ui_time_picker.h>
#include <list.h>
#include <time.h>
namespace OHOS {

class AlarmTimer : public Task {
    //获取系统时间和定时器时间；进行两个时间的判断；实现子视图的添加和删除
  public:
    AlarmTimer(UITimePicker *uitimer, RootView *root, UILabel *label,
               UILabel *label_tital, WeatherTimeView *tmView)
        : timer(uitimer), rooter(root), labeler(label),
          labeler_tital(label_tital), weatherTimeView(tmView) {
        flag_timer = 0;
        Task::Init();
    }

    ~AlarmTimer() {}

    void TaskStart() {
        Task::SetPeriod(1000); /* 1000=1s */
        Task::TaskExecute();
    }

    void Callback() override {

        const char *flag_h = new char[3];
        const char *flag_m = new char[3];

        int h = 0;
        int m = 0;
        int h_ = 0;
        int m_ = 0;

        struct tm rtcTime;

        int netConnect = SystemGetNetStatus();

        weatherTimeView->GetTime(&h, &m);

        if (netConnect != 1) {
            if (SystemGetTime(&rtcTime) < 0) {
                return;
            }

            h = rtcTime.tm_hour;
            m = rtcTime.tm_min;
        }
        flag_h = timer->GetSelectHour();
        flag_m = timer->GetSelectMinute();

        h_ = ((int)flag_h[0] - 0x30) * 10 + ((int)flag_h[1] - 0x30);
        m_ = ((int)flag_m[0] - 0x30) * 10 + ((int)flag_m[1] - 0x30);

        if (((h == h_) && (m == m_))) {
            if (flag_timer == 0) {

                labeler->SetText((char *)"日程时间到了!");

                flag_timer = 1;
            }
        } else {
            if (flag_timer == 1) {
                flag_timer = 0;
                labeler->SetText((char *)"              ");
            }
        }
    }

  private:
    WeatherTimeView *weatherTimeView{nullptr};
    RootView *rooter{nullptr};
    UITimePicker *timer{nullptr};
    UILabel *labeler{nullptr};
    UILabel *labeler_tital{nullptr};
    int flag_timer;
};

class ScheduleListTask : public Task {
  public:
    ScheduleListTask(WeatherTimeView *tmView, ScheduleListView *view,
                     List<ScheduleInfo> *list)
        : weatherTimeView(tmView), scheduleView(view), itemList(list) {
        curIdx = -1;
        Task::Init();
    }
    ~ScheduleListTask() {}

    void TaskStart() {
        Task::SetPeriod(1000); /* 1000=1s */
        Task::TaskExecute();
    }

    void SetList(List<ScheduleInfo> *list) { itemList = list; }

    void Callback() override {
        int idx = GetCurrentIdx();
        if (idx >= 0 && idx != curIdx) {
            SetScrollViewIdx(idx);
            curIdx = idx;
        }
    }

  private:
    List<ScheduleInfo> *itemList{nullptr};
    ScheduleListView *scheduleView{nullptr};
    WeatherTimeView *weatherTimeView{nullptr};
    static constexpr int ONE_HOUR = 60;
    int curIdx;

    int GetCurrentIdx() {
        int retval = -1;
        int i = 0;
        int hour = 0;
        int minute = 0;
        weatherTimeView->GetTime(&hour, &minute);
        if (itemList == nullptr || itemList->Size() == 0) {
            return -1;
        }

        ListNode<ScheduleInfo> *pNode = itemList->Head();
        for (i = 0; i < itemList->Size(); i++) {
            if (pNode == nullptr) {
                return -1;
            }

            if ((hour * ONE_HOUR + minute) <
                (pNode->data_.getEndTimeHours() * ONE_HOUR +
                 pNode->data_.getEndTimeMinutes())) {
                retval = i;
                break;
            }
            pNode = pNode->next_;
        }
        if (i == itemList->Size()) {
            retval = itemList->Size() - 1;
        }

        return retval;
    }

    void SetScrollViewIdx(int idx) {
        if (scheduleView == nullptr) {
            return;
        }
        scheduleView->ScheduleListViewSetSelect(idx);
        scheduleView->ScheduleListViewNotify();
    }
};

class ElectrBoardAbilitySlice : public AbilitySlice, public NativeBase {
  public:
    ElectrBoardAbilitySlice() = default;
    ~ElectrBoardAbilitySlice() override;

    void ScheduleUpdateList(ListHandle list);

  protected:
    void OnStart(const Want &want) override;
    void OnInactive() override;
    void OnActive(const Want &want) override;
    void OnBackground() override;
    void OnStop() override;

    void SetDateItemView();
    void SetScheduleView();
    void SetBackButton();
    void SetScrollView();
    void SetTitle();
    void SetTimerView();

  private:
    RootView *rootview_{nullptr};
    UILabel *labelTitle_{nullptr};

    UITimePicker *timer_{nullptr};
    AlarmTimer *alarmtimer_{nullptr};
    UILabel *alarmlabel_{nullptr};

    UIImageView *backButtonB_{nullptr};
    UIImageView *backButton_{nullptr};
    EventListener *buttonListener_{nullptr};

    UIViewGroup *dateInfo_{nullptr};
    WeatherTimeView *weatherTimeView_{nullptr};

    UIViewGroup *scheduleInfo_{nullptr};
    ScheduleListView *scheduleView_{nullptr};
    ScheduleListTask *scheduleTask_{nullptr};

    List<ScheduleInfo> scheduleList_;
};
} // namespace OHOS

#endif // __ELECTR_BOARD_ABILITY_SLICE_H__
